package com.core.website.util;

import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.crypto.MacSpi;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NewsInfo {
    public static void main(String[] args) {
        DataSource dataSource = new SimpleDriverDataSource(
                BeanUtils.instantiateClass(com.microsoft.sqlserver.jdbc.SQLServerDriver.class),
                "jdbc:sqlserver://152.136.213.120:1433;DataBase=com02",
                "sa",
                "Admin123!");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        DataSource dataSource2 = new SimpleDriverDataSource(
                BeanUtils.instantiateClass(com.microsoft.sqlserver.jdbc.SQLServerDriver.class),
                "jdbc:sqlserver://152.136.213.120:1433;DataBase=website",
                "sa",
                "Admin123!");
        JdbcTemplate jdbcTemplate2 = new JdbcTemplate(dataSource2);
        int count = jdbcTemplate.queryForObject("select count(1) from news_info",Integer.class);
        List<Map<String,Object>> columnList= jdbcTemplate2.queryForList("select * from core_column");
        Map<String,List<Map>> mapListGroup = columnList.stream().
                collect(Collectors.groupingBy(e->e.get("name").toString()));

        String querySql="select * from news_info ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(querySql);
        if(null!=maps&&maps.size()>0){
            List<String> batchSQL = new ArrayList<>();
            List<Map<String,Object>> errorMap=new ArrayList<>();
            for(int i=0;i< maps.size();i++){
                if(mapListGroup.keySet().contains(maps.get(i).get("BigClassName"))){
                    String columnId=mapListGroup.get(maps.get(i).get("BigClassName")).get(0).get("id").toString();
//                    String insertSql="insert into core_content_importa (id,title,content,create_time,column_id,author) " +
//                            "values(N'"+maps.get(i).get("id")+"'," +
//                            "N'"+maps.get(i).get("title")+"'," +
//                            "N'"+maps.get(i).get("content")+"'," +
//                            "'"+maps.get(i).get("fbdate")+"'," +
//                            "N'"+columnId+"'," +
//                            "N'"+maps.get(i).get("admin_per")+"')";
                    String insertSql="insert into core_content_importa (id,title,content,create_time,column_id,author) " +
                            "values(?,?,?,?,?,?)";
                    batchSQL.add(insertSql);
                    try {
                        jdbcTemplate2.update(insertSql,maps.get(i).get("id"),maps.get(i).get("title"),maps.get(i).get("content"),maps.get(i).get("fbdate"),columnId,maps.get(i).get("admin_per"));

                    }catch (Exception e){
                        Map<String, Object> error = maps.get(i);
                        error.put("error", e.getMessage());
                        errorMap.add(error);
                    }
                }

//                jdbcTemplate2.execute(insertSql);

            }
//            if (!batchSQL.isEmpty()){
//                int countNum = batchSQL.size() / 1000;
//                if(countNum>0){
//                    for(int i=0;i<countNum;i++){
//                        if((i+1)*1000<batchSQL.size()){
//                            List<String> strings = batchSQL.subList(i * 1000, (i + 1) * 1000);
//                            jdbcTemplate2.batchUpdate(strings.<String>toArray(new String[0]));
//                        }else{
//                            List<String> strings = batchSQL.subList(i * 1000, batchSQL.size());
//                            jdbcTemplate2.batchUpdate(strings.<String>toArray(new String[0]));
//                        }
//                    }
//                }else{
//                    jdbcTemplate2.batchUpdate(batchSQL.<String>toArray(new String[0]));
//                }
//
//
//
//            }
        }



    }

}
